package com.example.runnable;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {
	Context mContext;
	Button mLoadButton;
	ImageView mImageframe;
	
	Bitmap image;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		mContext = this;
		
		mImageframe = (ImageView) findViewById(R.id.image_frame);
		
		mLoadButton = (Button) findViewById(R.id.load_image_button);
		mLoadButton.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				
				Runnable r = new Runnable(){
					@Override
					public void run(){
						long startTime = System.currentTimeMillis();
						
						if(image != null){
							image.recycle();
						}
						
						image = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.space);
						
						mImageframe.post(new Runnable(){
							@Override
							public void run(){
								mImageframe.setImageBitmap(image);
							}
						});
						
						long time = System.currentTimeMillis()
												- startTime;
						
						Log.d("MainActivity","Time to load: "
									+ time + " milliseconds");
					}
				};
				
				new Thread(r).start();
				
			}
		});
	}

}
